package com.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.ProductDAO;
import com.dao.impl.ProductDAOImpl;
import com.model.Product;


/**
 * Servlet implementation class AddProductServlet
 */
public class AddProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		// 获取页面信息
		String productNo = request.getParameter("productNo");
		String productName = request.getParameter("productName");
		String price = request.getParameter("price");
		String description = request.getParameter("description");
		String state = request.getParameter("state");

		// 添加数据
		Product product = new Product();
		product.setProductNo(productNo);
		product.setProductName(productName);
		product.setPrice(Double.parseDouble(price));
		product.setDescription(description);
		product.setProductType_Id(Integer.parseInt(state));
		
		ProductDAO pd = new ProductDAOImpl();
		boolean b = pd.addProduct(product);

		String resultJson = null;

		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"添加成功！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"添加失败!\"}";
		}
		response.getWriter().print(resultJson);
	}

}
